/**
* @Title: caseAudit_list_service.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.services');
	app.service('CaseCommonService',['$http','BaseURL','$modal','Modal', function($http, baseUrl,$modal,Modal){
		
		var debug = false;
		var serv = this;
		
		
		/**
		 * 查询意见:
		 * taskCode,taskType,opinionType
		 */
		 serv.loadDispatchInfo = function(parameter){
				return $http({
			    	 method: "get",
			    	 url:baseUrl+'caseCommonDispatch/loadDispatchInfo.do',
			    	  params:parameter
				});
			
		}
		
		
		/**
		 * 下载稽核告知函数
		 */
		 serv.downAuditNoticationReport = function(parameter){
			 var caseCode=parameter.caseCode;
			 window.location =baseUrl+'auditDownFileController/downPdfFile.do?caseCode='+caseCode;

	        }
		
		/**
		 * 下载稽核报告
		 */
		 serv.downReportNoticPdfFile = function(parameter){
			 var caseCode=parameter.caseCode;
			 window.location =baseUrl+'auditDownFileController/downReportNoticPdfFile.do?caseCode='+caseCode;

	        }
		
		
		
		 /**
         * 根据事件状态，查询对应的所有事件 查询不与人相关
         */
        serv.loadProblemByStatus = function(parameter){
            return $http({
                method: "get",
                url:baseUrl+'auditDispatch/findProblemListByStatus.do',
                params:parameter
            });

        }
        
        
		 /**
         * 根据事件编码加载事件对象信息列表
         */
        serv.loadProblemInfo = function(parameter){
			
				return $http({
			    	 method: "get",
			    	 url:baseUrl+'auditDispatch/loadProblemInfo.do',
			    	  params:parameter
				});
			
		}
        
        /**
         * 查询事件待办任务
         */
        serv.loadProblemTask = function(parameter){
				return $http({
			    	 method: "get",
			    	 url:baseUrl+'auditDispatch/findProblemTaskList.do',
			    	  params:parameter
				});
			
		}
        /**
         * 查询事件调查（调查、复核）
         */
        serv.loadProblemAuditTask = function(parameter){
            return $http({
                method: "get",
                url:baseUrl+'auditDispatch/findProblemAuditTaskList.do',
                params:parameter
            });

        }
        /**
         * 查询案件待办任务
         */
        serv.loadCaseTask = function(parameter){
            return $http({
                method: "get",
                url:baseUrl+'auditDispatch/findCaseTaskList.do',
                params:parameter
            });
        }
        /**
         * 查询案件稽核待办任务（稽核、复核）
         */
        serv.loadCaseAuditTask = function(parameter){
            return $http({
                method: "get",
                url:baseUrl+'auditDispatch/findCaseAuditTaskList.do',
                params:parameter
            });
        }
        /**
         * 根据场景查询事件信息列表
         * sceneType 场景 自定义  
         *  [事件登记查询状态为(problemStatus=2,3) 设 sceneType = 23 ]
         * problemStatus 状态可自定义 *.problemStatus ='2','3','4'
         */
        serv.loadProblemsList = function(parameter){
        	return $http({
        		method: "get",
        		url:baseUrl+'/hcpms/audit/queryList.do',
        		params:parameter
        	});
        	
        }
        
        /**
         *  事件登记列表查询
         */
        serv.loadProblemList4Reg = function(parameter){
        	return $http({
        		method: "get",
        		url:baseUrl+'/hcpms/audit/queryProblemList4Reg.do',
        		params:parameter
        	});
        	
        }
		
		
		/**
		 * 根据事件编码加载医疗机构信息和医护人员和参保人员信息
		 * {
		 * problemNo:'',
		 * taskType：'1' //任务类型(1-事件2-案件)
		 * }
		 */
        serv.loadMedicalInfoList = function(parameter){
				return $http({
			    	 method: "get",
			    	 url:baseUrl+'auditDispatch/loadMedicalInfoList.do',
			    	  params:parameter
				});
			}
        
        /**
         * 根据事件编码加载事件对象信息
         */
        serv.loadProblemData = function(parameter){
			
				return $http({
			    	 method: "get",
			    	 url:baseUrl+'auditDispatch/loadProblemData.do',
			    	  params:parameter
				});
			
		}
        
        
       

        /**
         * 根据案件编码加载案件对象信息
         */
        serv.loadCaseData = function(parameter){
            return $http({
                method: "get",
                url:baseUrl+'auditProject/queryAuditCase.do',
                params:parameter
            });

        }

        
        /**
         * 
         * 增加审批意见或是修改,并修改案件表对应记录状态(onlysave==0 时候修改状态)
         * 
         * opinionTypeCurrent 当前 opinionType状态
         * statusAfter 需要修改状态(onlysave=0)时修改后的状态 
         * 根据案件编码操作
         */
        serv.approvementAuditOpinion = function(parameter){
        	return $http({
        		method: "post",
        		url:baseUrl+'auditOpinion/approvementAuditOpinion.do',
        		params:parameter
        	});
        } 
        
        /**
         *批量增加审批意见
         */
        serv.approvementAudits = function(parameter){
        	return $http({
        		method: "POST",
        		url:baseUrl+'auditOpinion/approvementAudits.do',
        		params:parameter
        	});
        } 
        
        
        /**
         * 增加审批意见或是修改,并修改事件表对应记录状态(onlysave==0 时候修改状态)
         * 
         * opinionTypeCurrent 当前 opinionType状态
         * statusAfter 需要修改状态(onlysave=0)时修改后的状态
         * 根据事件编码操作
         */
        serv.approvementProblemOpinion = function(parameter){
            return $http.post(baseUrl + 'auditOpinion/approvementProblemOpinion.do',parameter);
        }
        /**
         * 根据角色名称加载该角色下的用户信息
         * 传惨要求:
         * {
         * roleName:''
         * }
         * 返回对象：{data:[{}]}
         */
        serv.loadUserInfoByRoleName = function(parameter){
        	return $http({
        		method: "post",
        		url:baseUrl+'auditDispatch/loadUserInfoByRoleName.do',
        		params:parameter
        	});
        } 
        
        
        
        /**
         * 新增医疗机构的弹出框函数
         */
        serv.addMedicalFun=function($scope){
        	  var scope = $scope.$new();
 			 var modalInstance = $modal.open({
 			      templateUrl: baseUrl+'views/hcpms/audit/common/addMedicalDialog_view.html',
 			      backdrop:'static',
 				  size:'sm',
 				  scope:scope,
 				  controller: 'CaseCommonAddMedicalCtrl'
 			    });
        	
        }
        
      /**
        查看医护人员详细信息
       * 
       */
        serv.viewStaffList = function($scope){
			var scope = $scope.$new();
			scope.tableData=angular.isUndefined($scope.initStaffData)?$scope.staffData2:$scope.initStaffData;
			var modalInstance = $modal.open({
				templateUrl: baseUrl+'views/hcpms/audit/componentView/problemViewStaff_view.html',
				backdrop:'static',
				size:'sm',
				scope:scope,
				controller:function($scope,$modalInstance){
					//取消或或者直接关闭
					$scope.onOk = function(){
						$modalInstance.dismiss();
					}	
				}
			});
		}
        
        function loadAuditPointCutTemp(caseNo,targetType){
       	 var obj={
       			caseNo:caseNo,
       			targetType:targetType
			 };
       	 return $http({
        		method: "post",
        		url:baseUrl+'caseCommonDispatch/loadAuditPointCutTemp.do',
        		params:obj
        	});
       }
        
        /**
         * 新增医护人员弹出框函数
         */
        serv.addStaffFun=function($scope){
      	  var scope = $scope.$new();
			 var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/audit/common/addStaffDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller: 'CaseCommonAddStaffCtrl'
			    });
      	
        }
        
        /**
	        查看参保人员详细信息
         */
        serv.viewInsurList = function($scope){
			var scope = $scope.$new();
			scope.tableData=angular.isUndefined($scope.initInsuranceData)?$scope.insuranceData2 :$scope.initInsuranceData ;
			scope.columns =[
             {label:'社保号',map:'insuranceNo',width:'14%'},
			 {label:'姓名',map:'name',width:'10%'},
			 {label:'性别',map:'gender',width:'7%'},
			 {label:'年龄',map:'age',width:'7%'},
			 {label:'出生日期',map:'birthdayStr',width:'11%'},
			 {label:'医保卡号',map:'insuranceCardNo',width:'16%'},
			 {label:'身份证号',map:'idCard',width:'20%'},
			 {label:'人员类别',map:'personType',width:'15%'}];
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/audit/componentView/problemViewInsurance_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller:function($modalInstance,$scope){
					  $scope.onOk = function(){
				  		   $modalInstance.dismiss();
				  	   }	
				  }
			    });
			
		}
        
        
        /**
         * 新增参保人员弹出框函数
         */
        serv.addInsuranceFun=function($scope){
      	  var scope = $scope.$new();
			 var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/audit/common/addInsuranceDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller: 'CaseCommonAddInsuranceCtrl'
			    });
      	
        }
        
        
        /*****************************对医护人员的处理***************************/
          /**
           * 初始化临时的集合
           */
	      serv.initStaffAndInsuranceTempList=function($scope){
	    	  $scope.tempStaffList=new Array(); //临时保存医务人员信息
	  		  $scope.tempInsuranceList=new Array();//临时保存参保人信息
	  		  
	  		   if($scope.staffData2){
	  			    if($scope.staffData2.length>0){
	  			    	$scope.tempStaffList=$scope.staffData2.concat();
	  			    }
	  		   }else{
	  			 $scope.staffData2=[];
	  		   }
	  		   
	  		 if($scope.insuranceData2){
	  			    if($scope.insuranceData2.length>0){
	  			    	$scope.tempInsuranceList=$scope.insuranceData2.concat();
	  			    }
	  		   }else{
	  			 $scope.insuranceData2=[];
	  		   }
	  		  
	      }
	      
	      /**
	       * 新增医务人员后的回调执行函数
	       */
	      serv.addTempStaffList=function(array,$scope){
	    	  //判断当前的临时集合是否有重复的记录
	    	  
	    	  
	    	  
	    	  if(array.length>0){
	    		   $.each(array,function(index,item){
	    			   var obj={
	    					   name:item.name,
	    					   gender:item.gender,
	    					   idCard:item.idCard,
	    					   departName:item.departName,
	    					   applyDepartName:item.applyDepartName,
	    					   staffCode:item.staffCode,
	    					   isDeleted:'0',
	    					   flag:'0',
	    					   departCode:item.departCode,
	    					   medicalCode:item.medicalCode,
	    					   personType:item.personType,
	    					   medicalName:item.medicalName,
	    					   code:item.code,
	    					   selfCode:item.selfCode
	    			   };
	    			   
	    			   var checkFlag=false;
	    			   
	    			   if($scope.tempStaffList && $scope.tempStaffList.length>0){
	    				   
	    				   $.each($scope.tempStaffList,function(index,innerItem){
	    					   
	    					   var innerStaffCode=innerItem.staffCode;
	    					   var innnerFlag=innerItem.flag;
	    					   var innerIsDeleted=innerItem.isDeleted;
	    					   
	    					   
	    					   if(((innnerFlag && innnerFlag=='0') && (innerStaffCode==obj.staffCode)) 
	    						 ||((innerIsDeleted && innerIsDeleted=='0') &&(innerStaffCode==obj.staffCode))){
	    						   checkFlag=true;
	    						   return false;
	    					   }
	    				   });
	    				   
	    				   if(checkFlag){
	    					 
	    					   Modal.alert(obj.name+'已经存在了!');
	    					   return false;
	    				   }else{
	    					   
	    					    $scope.tempStaffList.push(obj);
				    			$scope.staffData2.push(obj); 
	    				   }
	    				   
	    				   
	    				   
	    			   }else{
	    				    $scope.tempStaffList.push(obj);
			    			$scope.staffData2.push(obj); 
	    			   }
	    			   
	   			        
	    		   });
		    			
	    	   }
	      }
	      /**
	       * 删除医务人员的回调执行函数
	       */
	      serv.deleteTempStaffList=function(data,$scope){
	    	  angular.forEach($scope.tempStaffList,function(staff,index){
	    			if (data ===  staff) {
	    				staff['flag']='1';
	    				staff['isDeleted']='1';
	                  }
	             });
	    	  angular.forEach($scope.staffData2,function(staff,index){
				if (data ===  staff) {
					$scope.staffData2.splice(index,1);
				}
			});
	      }
	      
	      /**
	       * 保存医务人员信息
	       */
	      serv.saveTempStaffListInfo=function($scope,taskType,targetType,taskCode){
	    	  
	    	      if($scope.saveType && $scope.saveType=='save'){
	    	    	  return serv.saveMedicalCommonInfo($scope.staffData2,taskType,targetType,taskCode);
	    	      }
	    	  
	    	    return serv.saveAuditTargetInfo($scope.staffData2,taskType,targetType,taskCode);
	      }
	      
	      /*****************************对参保人员的处理***************************/
	      /**
	       * 新增临时参保人员信息
	       */
	      serv.addTempInsuranceListInfo=function(array,$scope){
	    	  if(array.length>0){
	    		   $.each(array,function(index,item){
	    			   var obj={
	    					   name:item.name,
	    					   gender:item.gender,
	    					   idCard:item.idCard,
	    					   birthdayStr:item.birthdayStr,
	    					   insuranceNo:item.insuranceNo,
	    					   age:item.age,
	    					   isDeleted:'0',
	    					   flag:'0',
	    					   insuraceCode:item.insuraceCode,
	    					   medicalCode:item.medicalCode,
	    					   personType:item.personType,
	    					   insuranceCardNo:item.insuranceCardNo,
	    					   code:item.code,
	    					   selfCode:item.selfCode
	    			   };
	    			   
	    			   
	    			   
                         var checkFlag=false;
	    			   
	    			   if($scope.tempInsuranceList && $scope.tempInsuranceList.length>0){
	    				   
	    				   $.each($scope.tempInsuranceList,function(index,innerItem){
	    					   
	    					   var innerStaffCode=innerItem.selfCode;
	    					   var innnerFlag=innerItem.flag;
	    					   var innerIsDeleted=innerItem.isDeleted;
	    					   
	    					   if(((innnerFlag && innnerFlag=='0') && (innerStaffCode==obj.selfCode)) 
	    						 ||((innerIsDeleted && innerIsDeleted=='0') &&(innerStaffCode==obj.selfCode))){
	    						   checkFlag=true;
	    						   return false;
	    					   }
	    				   });
	    				   
	    				   if(checkFlag){
	    					 
	    					   Modal.alert(obj.name+'已经存在了!');
	    					   return false;
	    				   }else{
	    					   
	    					    $scope.tempInsuranceList.push(obj);
				    			$scope.insuranceData2.push(obj); 
	    				   }
	    				   
	    				   
	    				   
	    			   }else{
	    				    $scope.tempInsuranceList.push(obj);
			    			$scope.insuranceData2.push(obj); 
	    			   }
	    			   
	    			   
	    		   });
		    			
	    	   }
	      }
	      
	      /**
	       * 删除参保人员的回调执行函数
	       */
	      serv.deleteTempInsuranceInfoList=function(data,$scope){
	    	  angular.forEach($scope.tempInsuranceList,function(staff,index){
	    			if (data ===  staff) {
	    				staff['flag']='1';
	    				staff['isDeleted']='1';
	                  }
	             });
	    	  
	    	angular.forEach($scope.insuranceData2,function(insurance,index){
				if (data ===  insurance) {
					$scope.insuranceData2.splice(index,1);
				}
			});
	      }
	      /**
	       * 保存参保人员临时的集合信息
	       */
	      serv.saveTempInsuranceListInfo=function($scope,taskType,targetType,taskCode){
	    	     if($scope.saveType && $scope.saveType=='save'){
	    	    	  return serv.saveMedicalCommonInfo($scope.insuranceData2,taskType,targetType,taskCode);
	    	      }
	    	    return serv.saveAuditTargetInfo($scope.insuranceData2,taskType,targetType,taskCode);
	      }
        
        
        
        
        /**
         * 暂存稽核对象中医疗机构和医务人员信息
         * parameter={
         * taskCode:'',
         * taskType:'',1事件,2案件
         * targetType:'',对象类型(1-医疗机构2-医生3-参保人)
         * targetCode:''
         * }
         */
        serv.saveAuditTargetInfo=function(array,taskType,targetType,taskCode){
        	
        	var url=baseUrl+'caseCommonDispatch/saveAuditTargetInfo.do';
        	var paramter={
    				taskCode:taskCode,
    				taskType:taskType,
    				targetType:targetType
    		};
        	
        	if(array.length>0){
        		var parameterList=[];
        		$.each(array,function(index,item){
	   				 var obj={
	   						 taskCode:taskCode,
	   						 taskType:taskType,
	   						 targetType:targetType,
	   						 targetCode:item.selfCode
	   				 };
   				 parameterList.push(obj);
   	    	  });
        		
          if(parameterList.length==1){
        	  parameterList.push({taskCode:''});
          }
        	paramter.list=parameterList;
        	
        	}else{
        		paramter.list='';
        	}
        	
        	return $http({
        		method: "post",
        		url:url,
        		params:paramter
        	});
        	
        	
        }
        
        
        
        
        
        
        /**
         * 保存稽核对象中医疗机构和医务人员信息
         * parameter={
         * taskCode:'',
         * taskType:'',1事件,2案件
         * targetType:'',对象类型(1-医疗机构2-医生3-参保人)
         * targetCode:''
         * }
         */
        serv.saveMedicalCommonInfo=function(array,taskType,targetType,taskCode){
        	var url=baseUrl+'caseCommonDispatch/saveMedicalCommonInfo.do';
        	var paramter={
    				taskCode:taskCode,
    				taskType:taskType,
    				targetType:targetType
    		};
        	
        	if(array.length>0){
        		var parameterList=[];
        		$.each(array,function(index,item){
	   				 var obj={
	   						 taskCode:taskCode,
	   						 taskType:taskType,
	   						 targetType:targetType,
	   						 targetCode:item.selfCode
	   				 };
   				 parameterList.push(obj);
   	    	  });
        		
        	paramter.list=parameterList;
        	
        	  if(parameterList.length==1){
            	  parameterList.push({taskCode:''});
              }
        	
        	}else{
        		paramter.list='';
        	}
        	
        	return $http({
        		method: "post",
        		url:url,
        		params:paramter
        	});
        	
        	
        }
        
        
        /**
         * 查看案件信息中案件来源信息
         *  $scope.caseObj=caseObj;
         */
        serv.showCaseFromSourceDialog=function($scope){
        	 var scope = $scope.$new();
			 var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/audit/common/caseFromSourceDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller: 'CaseFromSourceDialogCtrl'
			    });
        }
        
        
        /**
         * 查看事件信息中案件来源信息
         *  $scope.problemObj=problemObj;
         */
        serv.showProblemFromSourceDialog=function($scope){
        	 var scope = $scope.$new();
			 var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/audit/common/problemFromSourceDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller: 'ProblemFromSourceDialogCtrl'
			    });
        }
        
        
        
        /**
         * 稽核报告对话框
         *  $scope.caseReportObj=caseReportObj;
         *  $scope.typeRead=true/false
         */
        serv.showCaseAuditReportDialog=function($scope){
        	 var scope = $scope.$new();
			 var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/audit/common/caseAuditReportDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller: 'CaseAuditReportDialogCtrl'
			    });
        }
        
        /**
         * 稽核机构数字点击
         *  
         *  fwaInvolve.medicalCountClick
         */
        serv.viewMedicalDetail=function($scope){
        	var scope = $scope.$new();
        	var _medicalData = angular.isUndefined($scope.initMedicalData)?$scope.medicalData2:$scope.initMedicalData ;
        	scope.tempData = _medicalData;
        	var modalInstance = $modal.open({
        		templateUrl: baseUrl+'views/hcpms/audit/componentView/problemViewMedical_view.html',
        		backdrop:'static',
        		size:'sm',
        		scope:scope,
        		controller: 'CaseAuditMedicalDetailCtrl'
        	});
        }
        
        
        /**
         * 稽核告知函对话框
         *  $scope.caseReportObj=caseReportObj;
         *  $scope.typeRead=true/false
         */
        serv.showCaseAuditTellReportDialog=function(scope){
			 var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/audit/common/caseAuditTellReportDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller: 'CaseAuditTellReportDialogCtrl'
			    });
        }
        
        /**
         * 查询所有意见信息
         */
        serv.queryAllOpinion = function(parameter){
        	return $http({
        		method: "post",
        		url:baseUrl+'auditOpinion/queryAllOpinion.do',
        		params:parameter
        	});
        } 
        
        
        /**
         * 加载稽核告知函的报告信息
         */
        serv.loadAuditNotifionInfo = function(parameter){
        	return $http({
        		method: "post",
        		url:baseUrl+'caseCheckAudit/loadAuditNotifiance.do',
        		params:parameter
        	});
        } 
        
        /**
         * 查询附件信息
         * @param id
         * @param callback
         */
        this.showImage = function(parameter){
            return $http({
                method:'GET',
                url:baseUrl + 'common/getFileInfo.do',
                params:parameter
            });
        };
        
        /**
         * 稽核审核撤回操作
         *  
         *  
         */
        serv.caseRollBack=function(param){
        	
        	 return $http({
                 method:'post',
                 url:baseUrl + 'auditManageRollBack/executeCaseRollBack.do',
                 params:param
             });
        };
        
	}]);
	
})();
